<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/12/2
 * Time: 23:31
 */

namespace App\Plugin\HostServer\Front;

use App\Plugin\HostServer\Models\PnHostSvCategory;
use App\Plugin\HostServer\Models\PnHostSvGood;
use App\Plugin\HostServer\Models\PnHostSvSearch;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Symfony\Component\Console\Input\Input;

class SearchController extends BaseController
{
    

    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/6 12:37
     * 函数说明:搜索页
     * 调用方式:get
     * @return mixed
     */
    public function index()
    {
        $items=PnHostSvSearch::where('checked',1)->orderBy('item_order','asc')->get()->toArray();
        $data=[];
        $title=config_cache_default('host_server.seo_title');
        $key=config_cache_default('host_server.seo_key',$title);
        $desc=config_cache_default('host_server.seo_desc',$title);
        $this->setSeo($title, $key, $desc);

        foreach($items as $k=>$v){

            if ($v['item_value'] =='money_sort'){
            	$data[$v['item_value']] = ['顺序','倒序'];
                continue;
            }            
            $data[$v['item_value']]=$this->getServerGoods($v['item_value']);
        }

        $data['items']=$items;
        return $this->display($data);
    }
    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/5 10:23
     * 函数说明:根据类型选择
     * 调用方式:get
     * @param $key
     * @return mixed
     */
    public function getServerGoods($key)
    {
        $list=PnHostSvGood::distinct()->orderBy($key,'asc')->get([$key])->toArray();

        $data=[];
        foreach ($list as $a){
            array_push($data,$a[$key]);
        }
        return $data;
    }


    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/9 20:16
     * 函数说明:获取服务器类型列表
     * 调用方式:get
     * @return mixed
     */
    public function getGoodSystems()
    {
        $list=PnHostSvCategory::where(['parent_id'=>0,'is_checked'=>1])->orderBy('sort','asc')->get(['name']);
        $data=[];
        foreach ($list as $v){
            array_push($data,$v->name);
        }
        return $data;
    }

    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/9 20:25
     * 函数说明:根据服务器name获取id
     * 调用方式:get
     * @param $name
     * @return mixed
     */
    public function getSystemIdByName($name)
    {
        $id=PnHostSvCategory::where('name',$name)->first()->id;
        return $id;
    }
    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/5 10:30
     * 函数说明:所有产品列表
     * 调用方式:get
     * @return mixed
     */
    public function list()
    {
        $data=Request::all();
        $data= json_decode($data['data'],true);

        if (!empty($data['money_sort']) ){
            if ($data['money_sort']=='从高到低'){
                $money_sort = 'DESC';
            }else{
                $money_sort = 'ASC';
            }
            unset($data['money_sort']);
        }
        //服务器类型id
        $systemId=0;
        //增加服务器类型筛选项
        $systemCondition=false;
        if(isset($data['support_system'])&&$data['support_system']!="全部"){
            $systemId=$this->getSystemIdByName($data['support_system']);
            $systemCondition=true;
            unset($data['support_system']);
        }
        //去除筛选条件中值为全部的条件
        $conditions=[];
        foreach($data as $k=>$v){
            $key=array_search($v,$data);
            $conditions[$key]=$data[$k];
        }
        $condition= $this->condition($conditions);

        //价格排序项
        if (!empty($money_sort) ){
            $list=PnHostSvGood::where($condition)->where('is_checked',1)->orderBy('money',$money_sort);
        }else{
            $list=PnHostSvGood::where($condition)->where('is_checked',1);
        }

        //系统筛选项判定
        if($systemCondition){
            $result=$list->where('support_system','like','%'.$systemId.'%')->get();
        }else{
            $result=$list->get();
        }

        $count = count($result);
        $json_data = array(
            "draw"          => 20,
            "recordsTotal"  => $count,
            "recordsFiltered" => $count ,
            "data"          => $result,
        );
        
        return $json_data;
    }
    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/6 13:22
     * 函数说明:函数功能描述
     * 调用方式:get  or  post
     * @return array
     */
    public function itemList()
    {
        $data=Request::all();
        $listArr=[];
        if(isset($data['order-money'])){
            unset($data['order-money']);
        }

        foreach($data['data'] as $k=>$v){
            if($v!="order-money"){
                if ($v =='money_sort'){
                    $listArr[$k]['value']=  ['从低到高','从高到低'];
                    $listArr[$k]['key']=$v ;
                    continue;
                }elseif ($v =='support_system'){
                    $listArr[$k]['value']=  $this->getGoodSystems();
                    $listArr[$k]['key']=$v ;
                    continue;
                }
                $listArr[$k]['value']=$this->getServerGoods($v);
                $listArr[$k]['key']=$v;
            }
        }
        return $listArr;
    }


    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/5 11:58
     * 函数说明:去除数组中存在值为全部的键值对
     * 调用方式:get
     * @param $conditions
     * @return mixed
     */
    public function condition($conditions)
    {
        foreach ($conditions as $v){
            if(in_array('全部',$conditions)){
                $key= array_search('全部',$conditions);
                unset($conditions[$key]);
            }
        }
        return $conditions;
    }
}
